<?php
// This file is used to insert a new comment via AJAX calls

// Headers are needed to force IE to not cache results.
header('Cache-Control: no-cache, no-store, must-revalidate'); //HTTP/1.1
header('Expires: Sun, 01 Jul 2005 00:00:00 GMT');
header('Pragma: no-cache'); //HTTP/1.0

require_once('../../../includes.php');

require_once('../model/page.php');
require_once('../model/comment.php');
require_once('../model/user.php');

global $CFG;

if ( !isloggedin() )
    trigger_error('You must be logged in to create a comment.', E_USER_ERROR);

$user = folio_user::SelectWhereIdent( $_SESSION['userid']);

// Grab basic vars.
$body = $_POST['body'];
$page_ident = intval( $_POST['page_ident']);

$page = folio_page::SelectWherePageIdent( $page_ident );
if ( !$page )
    trigger_error('Unable to load page "' . $page_ident . '"', E_USER_ERROR);
$owner = folio_user::SelectWhereIdent($page->user_ident);

// Set warning level to not show notices.
// Set back again after finished mailing, as PHP mailer will
//      throw a bunch of notices and screw up the postback.
$olderrorlevel = $CFG->debug;
$CFG->debug = 7;



// See what we should do with the comment, email or post online?
if( $_POST['private'] == 'false') {

    // Insert into DB
    $c = folio_comment::Insert($page, $body);
    $oPrivate = '';
} else {


    $oPrivate = "\n\n" .
                __gettext('This is a private comment, meaning it will not ' .
                          'be shown on your page or saved in the database. ' .
                          'This email is your only copy.') . "\n\n";

    // Email the comment to the person who made it.
    if( strlen($user->email) > 0 )
        email_to_user($user, $user, 'You Commented on ' . $page->title,
         '' . $CFG->sitename . "\n" .
         $CFG->wwwroot . "\n\n" .
         'You commented on page "' . $page->title . "\"\n" .
         url . $owner->username . '/page/' . folio_page::title_encode($page->title) .
         "\n\n\n" .
         $oPrivate .
         "====================================================================\n" .
         $body . "\n" .
         "====================================================================\n");

}

// Email
if( $owner->user_type == 'community' )
    $owner = folio_user::SelectWhereIdent( $page->creator_ident);

// Send to the
if( strlen($owner->email) > 0 )
    email_to_user($owner, $user, 'Comment on ' . $page->title,
         'Email from ' . $CFG->sitename . "\n" .
         $CFG->wwwroot . "\n\n" .
         'The user "' . $user->username . '" commented on your page "' .
         $page->title . "\"\n" .
         url . $owner->username . '/page/' . folio_page::title_encode($page->title) .
         "\n\n\n" .
         $oPrivate .
         "====================================================================\n" .
         $body . "\n" .
         "====================================================================\n");


// Set back again after finished mailing.
$CFG->debug = $olderrorlevel;



echo "true";

?>
